// UserLogin.hpp
// Created by Limou on 24-10-6.

#ifndef FRONTEND_USERLOGIN_HPP
#define FRONTEND_USERLOGIN_HPP

#include <QWidget>
#include <QDialog>
#include <QGuiApplication>
#include <QScreen>
#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>

namespace chat_system::window {
    class UserLogin : public QDialog {
    private:
        // 组件
        QLabel* logoLab_;
        QPixmap pixmapOfLogoLab_;
        QLineEdit* usernameEdit_;
        QLineEdit* passwordEdit_;
        QLineEdit* verifyCodeEdit_;
        QPushButton* verifyCodeWidget_;
        QLabel* titleLab_;
        QPushButton* submitBtn_;
        QPushButton* phoneModeBtn_;
        QPushButton* switchModeBtn_;

        // 布局
        QGridLayout* gLayoutOfSelf_;

        // 数据
        // ...

    public:
        UserLogin()
        : QDialog()
        , logoLab_(new QLabel(this))
        , pixmapOfLogoLab_()
        , usernameEdit_(new QLineEdit(this))
        , passwordEdit_(new QLineEdit(this))
        , verifyCodeEdit_(new QLineEdit(this))
        , verifyCodeWidget_(new QPushButton(this))
        , titleLab_(new QLabel(this))
        , submitBtn_(new QPushButton(this))
        , phoneModeBtn_(new QPushButton(this))
        , switchModeBtn_(new QPushButton(this))
        , gLayoutOfSelf_(new QGridLayout()) {
            this->setElement();
            this->setElementStaticData();
            this->setElementSize();
            this->setElementStyle();
            this->setElementLayout();
            this->setElementSignalSlot();
            this->setElementShow();
        }

        ~UserLogin() override = default;

    private:
        // 设置组件方法
        void setElement() {
            pixmapOfLogoLab_ = QPixmap(":/window/window_main/logo.png");
            logoLab_->setPixmap(pixmapOfLogoLab_);

            // 设置关闭时自动销毁
            this->setAttribute(Qt::WA_DeleteOnClose);

            // 设置移动到屏幕正中间
            QRect screenGeometry = QGuiApplication::primaryScreen()->geometry();
            int x = (screenGeometry.width() - this->width()) / 2;
            int y = (screenGeometry.height() - this->height()) / 2;
            this->move(x, y);

            titleLab_->setAlignment(Qt::AlignCenter);
            usernameEdit_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
            passwordEdit_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
            passwordEdit_->setEchoMode(QLineEdit::Password);
            verifyCodeEdit_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

            TODO("此处先用 QPushButton 来表示一下, 后续会自定义 QWidget, 通过画图 api 来实现这里的验证码功能");
            verifyCodeWidget_->setText("验证码");

            submitBtn_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
            phoneModeBtn_->setText("手机号登录");
        }

        // 设置处理数据方法
        void setElementStaticData() {
            this->setWindowTitle("登录");
            this->setWindowIcon(QIcon(":/window/window_main/logo.png"));
            titleLab_->setText("登录");
            usernameEdit_->setPlaceholderText("输入用户名");
            passwordEdit_->setPlaceholderText("输入密码");
            verifyCodeEdit_->setPlaceholderText("输入验证码");
            submitBtn_->setText("登录");
            switchModeBtn_->setText("注册");
        }

        // 设置组件尺寸方法
        void setElementSize() {
            int width = 100;
            int height = 100;

            this->setFixedSize(350, 457);
            logoLab_->setFixedSize(150, 150);
            logoLab_->setPixmap(pixmapOfLogoLab_.scaled(width, height, Qt::KeepAspectRatio));
            titleLab_->setFixedHeight(50);
            usernameEdit_->setFixedHeight(40);
            passwordEdit_->setFixedHeight(40);
            verifyCodeEdit_->setFixedHeight(40);
            submitBtn_->setFixedHeight(40);
            phoneModeBtn_->setFixedSize(100, 40);
            switchModeBtn_->setFixedSize(100, 40);
        }

        // 设置组件风格方法
        void setElementStyle() {
            QString editStyle = "QLineEdit { color: black; border: none; border-radius: 5px; font-size: 15px; background-color: rgb(240, 240, 240); padding-left:5px; }";
            QString btnGreenStyle = "QPushButton { border: none; border-radius: 5px; background-color: rgb(44, 182, 61); color: rgb(255, 255, 255); }";
            btnGreenStyle += "QPushButton:pressed { background-color: rgb(240, 240, 240); }";
            QString btnWhiteStyle = "QPushButton { border: none; border-radius: 5px; background-color: transparent; }";
            btnWhiteStyle += "QPushButton:pressed { background-color: rgb(240, 240, 240); }";

            this->setStyleSheet("QDialog { background-color: white; }");

            titleLab_->setStyleSheet("QLabel { font-size: 40px; font-weight: 600; }");
            usernameEdit_->setStyleSheet(editStyle);
            passwordEdit_->setStyleSheet(editStyle);
            verifyCodeEdit_->setStyleSheet(editStyle);
            verifyCodeWidget_->setStyleSheet("QWidget { color: black; border: none; }");
            submitBtn_->setStyleSheet(btnGreenStyle);
            phoneModeBtn_->setStyleSheet(btnWhiteStyle);
            switchModeBtn_->setStyleSheet(btnWhiteStyle);
        }

        // 设置组件布局方法
        void setElementLayout() {
            // 初始本组件布局管理器
            gLayoutOfSelf_->setSpacing(0);
            gLayoutOfSelf_->setVerticalSpacing(10);
            gLayoutOfSelf_->setContentsMargins(30, 10, 30, 10);
            this->setLayout(gLayoutOfSelf_);

            // 设置本组件布局管理器
            logoLab_->setAlignment(Qt::AlignCenter); // 设置 QLabel 的对齐方式为居中
            gLayoutOfSelf_->addWidget(logoLab_, 0, 0, 1, 5, Qt::AlignCenter);
            gLayoutOfSelf_->addWidget(titleLab_, 1, 0, 1, 5);
            gLayoutOfSelf_->addWidget(usernameEdit_, 2, 0, 1, 5);
            gLayoutOfSelf_->addWidget(passwordEdit_, 3, 0, 1, 5);
            gLayoutOfSelf_->addWidget(verifyCodeEdit_, 4, 0, 1, 4);
            gLayoutOfSelf_->addWidget(verifyCodeWidget_, 4, 4, 1, 1); // 验证码
            gLayoutOfSelf_->addWidget(submitBtn_, 5, 0, 1, 5);
            gLayoutOfSelf_->addWidget(phoneModeBtn_, 6, 0, 1, 1);
            gLayoutOfSelf_->addWidget(switchModeBtn_, 6, 4, 1, 1);
        }

        // 设置组件信号槽方法
        void setElementSignalSlot() {
            connect(submitBtn_, &QPushButton::clicked, this, [this]() {
                TODO("获取输入框中的用户凭证提交到远端, 返回 cookie 验证, 以后所有的网络请求都需要携带这个 cookie");
                TODO("而这个 cookie 在网络传输中如果希望不被截取, 就需要做 https");
            });
        }

        // 设置组件显示方法
        void setElementShow() {}
    };
}

#endif // FRONTEND_USERLOGIN_HPP
